package org.marketing.common.dms.http;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.marketing.common.FrameHttpUtil;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.dms.dto.IfMessage;
import org.marketing.common.dms.dto.ResultBase;
import org.marketing.common.dms.dto.ResultMessageDetail;
import org.marketing.common.exception.UtilException;
import org.marketing.common.util.MD5Util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class HttpCommonUtil {
	private static Logger logger = Logger.getLogger(HttpCommonUtil.class);

	/**
	 * 调用dms的接口
	 * 
	 * @param map
	 * @param ifCode
	 * @param url
	 * @param t
	 * @return
	 */
	public static <T> List<T> getOwnerInfo(Map<String, Object> map, String ifCode, String url, Class<T> t){
		logger.info("---调用dcms的接口--");
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(map);
		String time = String.valueOf(new Date().getTime());
		String transferNo = MD5Util.MD5(ifCode + time + CommonConstant.HTTP_CLUB_KEY);
		IfMessage<List<Map<String, Object>>> para = new IfMessage<>(ifCode, transferNo, time,1);
		para.setIfData(list);
		String json = JSON.toJSONString(para);
		Map<String, String> param = new HashMap<>();
		param.put("data", json);
		String result = FrameHttpUtil.post(url, param);
		if ("".equals(result)) {
			// 网络调用异常
			throw new UtilException(CommonConstant.HTTP_CLUB_ERROR);
		} else {
			ResultBase rs = JSON.parseObject(result, ResultBase.class);
			String code = rs.getData().getResult();
			if ("200".equals(code)||(code==null&&rs.getData().getResultErrList() ==null)) {
				// 调用接口正常
				List<T> data = JSONObject.parseArray(rs.getData().getResultList(), t);
                logger.info("返回结果长度："+data.size());
				logger.info("返回结果："+data.toString());
				return data;
			}else if("300".equals(code)){
                //车辆信息不存在或者该车辆尚未实销！
                List<T> data = null;
                return data;
			}
			else {
				// 返回异常
				ResultMessageDetail mess = null;
				if (rs.getData().getResultErrList() != null && rs.getData().getResultErrList().size() > 0) {
					mess = rs.getData().getResultErrList().get(0);
					throw new UtilException(mess.toString());
				}
				throw new UtilException(code);
			}

		}
	}
	
	public static <T> List<T> getOwnerInfos(List<Map<String, Object>> map, String ifCode, String url, Class<T> t){
        logger.info("---调用dcms的接口--");
        String time = String.valueOf(new Date().getTime());
        String transferNo = MD5Util.MD5(ifCode + time + CommonConstant.HTTP_CLUB_KEY);
        IfMessage<List<Map<String, Object>>> para = new IfMessage<>(ifCode, transferNo, time, map.size());
        para.setIfData(map);
        String json = JSON.toJSONString(para);
        Map<String, String> param = new HashMap<>();
        param.put("data", json);
        logger.info("发送参数>>>>>>>>"+param);
        String result = FrameHttpUtil.post(url, param);
        if ("".equals(result)) {
            // 网络调用异常
            throw new UtilException(CommonConstant.HTTP_CLUB_ERROR);
        } else {
            ResultBase rs = JSON.parseObject(result, ResultBase.class);
            String code = rs.getData().getResult();
            if ("200".equals(code)||(code==null&&rs.getData().getResultErrList() ==null)) {
                // 调用接口正常
                List<T> data = JSONObject.parseArray(rs.getData().getResultList(), t);
                logger.info("返回结果长度："+data.size());
                logger.info("返回结果："+data.toString());
                return data;
            }else if("300".equals(code)){
                // 调用接口正常
                List<T> data = JSONObject.parseArray(rs.getData().getResultList(), t);
                logger.info("返回结果长度："+data.size());
                logger.info("返回结果："+data.toString());
                return data;
            }
            else {
                // 返回异常
                ResultMessageDetail mess = null;
                if (rs.getData().getResultErrList() != null && rs.getData().getResultErrList().size() > 0) {
                    mess = rs.getData().getResultErrList().get(0);
                    throw new UtilException(mess.toString());
                }
                throw new UtilException(code);
            }

        }
    }
}
